package com.hui.util;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

/**
 * 
 * ============================================================
 * 
 * copyright ZENG　HUI (c) 2014
 * 
 * author : HUI
 * 
 * version : 1.0
 * 
 * date created : On November 24, 2014 10:03:44
 * 
 * description : A utility class about the network
 * 
 * revision history :
 * 
 * ============================================================
 * 
 */
public class NetManagerUtil {
	// 网络错误
	public static final int NET_ERROR = 500;

	// Determine whether the network of the available methods
	public static boolean isOpenNetwork(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null)
				for (int i = 0; i < info.length; i++)
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
		}
		return false;
	}

	// Determine whether to open the WIFI network
	public static boolean isOpenWifi(Context context) {
		ConnectivityManager connManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mWifi = connManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		return mWifi.isConnected();
	}

	// Users to enter the network Settings interface
	public static void enterSettings(Context context) {
		Intent intent = new Intent("/");
		ComponentName cm = new ComponentName("com.android.settings",
				"com.android.settings.Settings");
		intent.setComponent(cm);
		intent.setAction("android.intent.action.VIEW");
		context.startActivity(intent);
	}

	/**
	 * Judge whether the GPS, GPS or AGPS open a is considered to be open
	 * 
	 * @param context
	 * @return true According to open
	 */
	public static final boolean isOpenGps(final Context context) {
		LocationManager locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		// 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
		boolean gps = locationManager
				.isProviderEnabled(LocationManager.GPS_PROVIDER);
		// 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
		boolean network = locationManager
				.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		if (gps || network) {
			return true;
		}
		return false;
	}

	/**
	 * Said opening force to help the user to open the GPS
	 * 
	 * @param context
	 */
	public static final void openGPS(Context context) {
		Intent GPSIntent = new Intent();
		GPSIntent.setClassName("com.android.settings",
				"com.android.settings.widget.SettingsAppWidgetProvider");
		GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
		GPSIntent.setData(Uri.parse("custom:3"));
		try {
			PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
		} catch (CanceledException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 用ping的方式判断网络是否可用
	 * 
	 * @return
	 */
	public static final boolean pingNetIsUsable(Context context) {
		if (is3G(context)) {
			return true;
		}
		try {
			String ip = "www.baidu.com";// 除非百度挂了，否则用这个应该没问题~
			Process p = Runtime.getRuntime().exec("ping -c 1 -w 1 " + ip);// ping1次
			// PING的状态
			int status = p.waitFor();
			if (status == 0 || status == 1) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 当前是否是3G网络
	 * 
	 * @param context
	 * @return
	 */
	public static boolean is3G(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null
				&& (activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE || activeNetInfo
						.getType() == ConnectivityManager.TYPE_WIFI)) {
			return true;
		}
		return false;
	}

	/**
	 * 得到WIFI的IP地址
	 * @param context
	 * @return
	 */
	public static String getWiFiIpAddress(Context context) {
		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		// 获取32位整型IP地址
		int ipAddress = wifiInfo.getIpAddress();

		// 返回整型地址转换成“*.*.*.*”地址
		return String.format("%d.%d.%d.%d", (ipAddress & 0xff),
				(ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),
				(ipAddress >> 24 & 0xff));
	}
	
	/**
	 * 当前网络是否可用
	 * @return
	 */
	public static final boolean cureentNetIsAvailable(Context context){
		// 先判断有没有打开网络
		boolean netAvailable =  isOpenNetwork(context);
				
		if(isOpenWifi(context)){
			// 如果打开的是WIFI
			String ipAddress = getWiFiIpAddress(context);
			netAvailable = !ipAddress.equals("0.0.0.0");
		}		
		return netAvailable;
	}
}
